/**
 * Copyright 2013 Douglas Lacher
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * @author Douglas Lacher <doug.lacher@gmail.com>
 */

package com.devioustoast.andmoco;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeSet;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;

public class MovesFragment extends Fragment {

  public static final String LOG_TAG = "MovesFragment";

  private Robot mRobot;

  public MovesFragment() {}

  @Override
  public View onCreateView(
      LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View movesView = inflater.inflate(R.layout.fragment_moves, container, false);
    if (mRobot != null) {
      List<Map<String, Runnable>> runnables = new ArrayList<Map<String, Runnable>>();
      // The order in which the runnables are added is significant.  First in; first out.
      runnables.add(mRobot.getOperationsMap());
      runnables.add(mRobot.getMovesMap());
      GridView movesContainer = (GridView) movesView.findViewById(R.id.moves_container);
      movesContainer.setAdapter(new ButtonAdapter(getActivity(), runnables));
    }
    return movesView;
  }

  public void setRobot(Robot robot) {
    mRobot = robot;
  }

  public class ButtonAdapter extends BaseAdapter {

    private Context mContext;
    private SparseArray<Button> mButtons;

    public ButtonAdapter(Context context, List<Map<String, Runnable>> runnables) {
      mContext = context;
      mButtons = generateButtons(runnables);
    }

    @Override
    public int getCount() {
      return mButtons.size();
    }

    @Override
    public Button getItem(int position) {
      return mButtons.get(position);
    }

    @Override
    public long getItemId(int position) {
      return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
      Button button;
      if (convertView == null) {
        button = mButtons.get(position);
      } else {
        button = (Button) convertView;
      }
      return button;
    }

    private SparseArray<Button> generateButtons(List<Map<String, Runnable>> runnables) {
      SparseArray<Button> buttonArray = new SparseArray<Button>(runnables.size());
      int i = 0;
      for (Map<String, Runnable> map : runnables) {
        TreeSet<String> names = new TreeSet<String>(map.keySet());
        for (String name : names) {
          final Runnable move = map.get(name);
          Button button = new Button(mContext);
          button.setText(name);
          button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
              move.run();
            }});
          buttonArray.append(i, button);
          i++;
        }
      }
      return buttonArray;
    }
  }
}
